package com.poker.monitor.model;

import android.graphics.Bitmap;
import android.util.Log;

import com.poker.monitor.ocr.OcrCallback;
import com.poker.monitor.ocr.OcrException;
import com.poker.monitor.ocr.OcrService;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class GameState {
    private List<Player> players;  // 所有玩家
    private List<PokerCard> communityCards;  // 公共牌
    private double pot;  // 当前底池
    private int dealerPosition;  // 庄家位置
    private int currentPlayerPosition;  // 当前行动玩家位置
    private String gamePhase;  // 当前游戏阶段（preflop, flop, turn, river）

    public GameState() {
        players = new ArrayList<>();
        communityCards = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            players.add(new Player(i));
        }
    }

    public List<Player> getPlayers() {
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    public List<PokerCard> getCommunityCards() {
        return communityCards;
    }

    public void setCommunityCards(List<PokerCard> communityCards) {
        this.communityCards = communityCards;
    }

    public double getPot() {
        return pot;
    }

    public void setPot(double pot) {
        this.pot = pot;
    }

    public int getDealerPosition() {
        return dealerPosition;
    }

    public void setDealerPosition(int dealerPosition) {
        this.dealerPosition = dealerPosition;
    }

    public int getCurrentPlayerPosition() {
        return currentPlayerPosition;
    }

    public void setCurrentPlayerPosition(int currentPlayerPosition) {
        this.currentPlayerPosition = currentPlayerPosition;
    }

    public String getGamePhase() {
        return gamePhase;
    }

    public void setGamePhase(String gamePhase) {
        this.gamePhase = gamePhase;
    }

    public void updateGameState(List<Bitmap> playerRegions, Bitmap communityCards, Bitmap pot, OcrService ocrService) {
        // 异步更新玩家信息
        for (int i = 0; i < playerRegions.size(); i++) {
            int finalI = i;
            ocrService.recognizeText(playerRegions.get(i), new OcrCallback() {
                @Override
                public void onSuccess(JSONArray results) {
                    String playerInfo = results.optString(0);
                    updatePlayerInfo(finalI, playerInfo);
                }

                @Override
                public void onFailure(OcrException exception) {
                    Log.e("GameState", "OCR识别玩家信息失败", exception);
                }
            });
        }

        // 更新公共牌信息
        ocrService.recognizeText(communityCards, new OcrCallback() {
            @Override
            public void onSuccess(JSONArray results) {
                String communityCardsInfo = results.optString(0);
                updateCommunityCards(communityCardsInfo);
            }

            @Override
            public void onFailure(OcrException exception) {
                Log.e("GameState", "OCR识别公共牌失败", exception);
            }
        });

        // 更新底池信息
        ocrService.recognizeText(pot, new OcrCallback() {
            @Override
            public void onSuccess(JSONArray results) {
                String potInfo = results.optString(0);
                updatePotInfo(potInfo);
            }

            @Override
            public void onFailure(OcrException exception) {
                Log.e("GameState", "OCR识别底池失败", exception);
            }
        });

        // 通知观察者状态已更新
        notifyStateChanged();

    }

    private void updatePlayerInfo(int playerIndex, String info) {
        // 解析玩家信息（筹码、手牌等）
        // TODO: 实现具体的解析逻辑
    }

    private void updateCommunityCards(String info) {
        // 解析公共牌信息
        // TODO: 实现具体的解析逻辑
    }

    private void updatePotInfo(String info) {
        // 解析底池信息
        // TODO: 实现具体的解析逻辑
    }

    private void notifyStateChanged() {
        // 通知观察者游戏状态已更新
        // TODO: 实现观察者通知逻辑
    }

    public Player getPlayerByPosition(int position) {
        for (Player player : players) {
            if (player.getPosition() == position) {
                return player;
            }
        }
        return null;
    }
}